package com.goddess.yeecolor.activity.myFragmentItemActivityAll;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.MainActivity;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.view.dialog_progress;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/16
 * 作用: ******
 * 备注: 支付成功页面
 */
public class JoinSuccessActivity extends BaseActivity {

    @InjectView(R.id.title_back)
    ImageView titleBack;
    @InjectView(R.id.register_enroll)
    Button registerEnroll;
    private String state;
    @InjectView(R.id.image)
    ImageView imageView;
    @InjectView(R.id.cgLinearlayout)
    LinearLayout cgLinearlayout;
    @InjectView(R.id.errLinearlayout)
    LinearLayout errLinearlayout;
    @InjectView(R.id.join_name)
    TextView err_name;
    @InjectView(R.id.name)
    TextView name;
    @InjectView(R.id.number)
    TextView number;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_joinsuccess);
        ButterKnife.inject(this);
        state=getIntent().getStringExtra("tag");

        initFindView();
        initData();

    }

    @Override
    public void initOnClck() {

    }

    private String names;
    @Override
    public void initFindView() {


    }
    private Map<Object, Object> map;
    @Override
    public void initData() {
        setProgress();
        map=new HashMap<Object, Object>();
        map.put("unique", SharePreceUtils.getOther("user","unique"));
        OkttpUtils.getIntence().OkttpPost(MyUrl.BUYSHOWJOIN,map,handler,1);
    }

    @OnClick({R.id.title_back, R.id.register_enroll})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back: //标题栏退出按钮
                finish();
                break;
            case R.id.register_enroll://底部按钮
                SharePreceUtils.saveOne("type",2,"show");//返回课程
                JumpIntents.startIntent(JoinSuccessActivity.this, MainActivity.class);
                break;
        }
    }

    private String jsonString,states,typed;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");
                JSONObject data=new JSONObject(jsonString);
                JSONObject result=data.getJSONObject("result");
                if (data.getInt("code")==100) {
                    states = result.optString("state");
                    names = SharePreceUtils.getOther("user", "nickname");
                    if (states.equals("1")) {
                        imageView.setImageResource(R.mipmap.dui);
                        cgLinearlayout.setVisibility(View.VISIBLE);
                        errLinearlayout.setVisibility(View.INVISIBLE);
                        name.setText(names);
                    } else if (states.equals("0")) {
                        //支付失败
                        imageView.setImageResource(R.mipmap.nonono);
                        errLinearlayout.setVisibility(View.VISIBLE);
                        cgLinearlayout.setVisibility(View.INVISIBLE);
                        err_name.setText(names);
                        registerEnroll.setVisibility(View.GONE);
                    }

                }
                if (dialog_progress!=null&& dialog_progress.isShowing()){
                    dialog_progress.dismiss();
                }

            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    };
    private dialog_progress dialog_progress;
    private void setProgress(){
        dialog_progress=new dialog_progress(this);
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }
}
